package com.stj.dao;

import com.stj.entity.User;
import com.stj.util.JdbcUtil;

import java.sql.*;
import java.util.ArrayList;

public class UserDao {
        Connection conn = JdbcUtil.getConnection();
        public int Login(User user) {
            int i=0;
            try {
                String sql = "select * from t_blogger where username=? and password = ?";
                PreparedStatement pst = conn.prepareStatement(sql);
                pst.setString(1,user.getUserName());
                pst.setString(2,user.getPassword());
                ResultSet rs = pst.executeQuery();
                if(rs.next())i=1;
            } catch (Exception e) {
                System.out.println("UserDao -> Login方法错误" + e);
            }
            return i;
    }

    public int register(User user) {
        int i=0;
        try{
            String sql = "insert into t_blogger(userName,password) VALUES(?,?)";
            PreparedStatement pst = conn.prepareStatement(sql);
            pst.setString(1,user.getUserName());
            pst.setString(2,user.getPassword());
            i =pst.executeUpdate();
            return i;
        }catch (Exception e){
            System.out.println("UserDao -> register方法错误" + e);
        }
        return i;
    }

    public User selectOneBlogger(User user) {
        try {
            Connection conn = JdbcUtil.getConnection();
            String sql = "SELECT * FROM t_blogger where userName=? And password=?";
            PreparedStatement pst = conn.prepareStatement(sql);
            pst.setString(1,user.getUserName());
            pst.setString(2,user.getPassword());
            ResultSet rs = pst.executeQuery();
            if (rs.next()){
                User blogger = new User(rs.getInt(1),rs.getString(2),
                        rs.getString(3),rs.getString(4),rs.getString(5),
                        rs.getString(6),rs.getString(7));
                return blogger;
            }
        } catch (Exception e) {
            System.out.println("UserDao -> selectOneBlogger" + e);
        }
        return null;
    }
}
